package com.qf.controller;

import com.qf.pojo.Admin;
import com.qf.pojo.PageBean;
import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.impl.AdminServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 管理员模块
 */
@WebServlet("/admin")
public class AdminServlet extends BaseServlet {


    /**
     * 管理员登录功能
     * @param request 请求
     * @param response 响应
     * @throws IOException
     * @throws ServletException
     */
    public void adminLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {

        //如何接收表单提交的参数?
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        request.getSession().setAttribute("msg","hello");
        //访问业务接口
        AdminService adminService = new AdminServiceImpl() ;
        Admin admin = adminService.getAdminByusename(username, password);
        if(admin!=null){
            //将admin实体存储在HttpSession域中
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin) ;
            //重定向到管理员 的主页
            response.sendRedirect(request.getContextPath()+"/admin/admin_index.jsp");
        }else{
            //给request域中存数据
            request.setAttribute("msg","用户名或者密码输入错误");
            //请求转发到admin/admin_login.jsp
            request.getRequestDispatcher("/admin/admin_login.jsp").forward(request,response);
        }
    }

    /**
     * 管理员退出
     * @param request 请求对象
     * @param response 响应对象
     */
    public void logOut(HttpServletRequest request,HttpServletResponse response) throws IOException {


        //从request对象中获取HttpSession
        HttpSession session = request.getSession();
        //public void invalidate()
        //使此会话无效，然后取消对任何绑定到它的对象的绑定。
        session.invalidate() ;
        //重定向管理员主页
        response.sendRedirect(request.getContextPath()+"/admin/admin_index.jsp");
    }

    /**
     * 管理员分页查询用户
     * @param request 请求对象
     * @param response 响应对象
     */
    public void getUserByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        // <a href="${pageContext.request.contextPath}/admin?methodName=getUserByPage
        //接收地址栏携带的参数
        String curPage = request.getParameter("curPage");
        if(curPage==null || "".equals(curPage)){ //没有内容
            curPage = "1";
        }
        String pSize = request.getParameter("pSize");
        if(pSize ==null || "".equals(pSize)){
            pSize = "4" ;
        }
        //转换int类型
        int currentPage = Integer.parseInt(curPage);
        int pageSize = Integer.parseInt(pSize) ;
        System.out.println(currentPage+"---"+pageSize);

        //调用管理员业务接口
        AdminService adminService = new AdminServiceImpl() ;
        PageBean<User> pb = adminService.getUserByPage(currentPage, pageSize);
        System.out.println(pb);
        //将分页实体存储在request域中
        request.setAttribute("pb",pb) ;
        //请求转发到 /admin/user/user_list.jsp
        request.getRequestDispatcher("/admin/user/user_list.jsp").forward(request,response);
    }

    /**
     * 通过用户uid查询指定用户(管理员修改第一步操作)
     * @param request 请求
     * @param response 响应
     */
    public void findUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //window.location.href="${pageContext.request.contextPath}/admin?methodName=findUser&uid="+uid
        //1)获取get请求过来的uid这个参数
        String uid = request.getParameter("uid") ;
        System.out.println(uid) ;
        //2)调用AdminService业务接口
        AdminService adminService = new AdminServiceImpl() ;
        User user = adminService.getUser(uid);
        //3)将user对象存储在request域中
        request.setAttribute("user",user) ;
        //4)请求转发到/admin/user/user_update.jsp
        request.getRequestDispatcher("/admin/user/user_update.jsp").forward(request,response);

    }

    /**
     * 管理员通过表单数据修改用户
     * @param request 请求
     * @param response 响应
     */
    public void updateUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //解决post提交的中文乱码
        // request.setCharacterEncoding("utf-8") ;
        //接收参数
       /* String uid = request.getParameter("uid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String sex = request.getParameter("sex") ;*/
        Map<String, String[]> map = request.getParameterMap();
        //封装User对象
        User user = new User() ;
        /*user.setUsername(username) ;
        user.setUid(uid) ;
        user.setPassword(password) ;
        user.setName(name) ;
        user.setEmail(email) ;
        user.setSex(sex) ;*/
       /* commons-beanutils.jar包: 里面工具类BeanUtils--->提供静态方法
        public static void populate(Object bean, Map properties)
        参数1:就是实体对象(bean对象)
        参数2:获取的map集合*/
        try {
            BeanUtils.populate(user,map) ;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(user) ;


        //调用业务接口
        AdminService adminService = new AdminServiceImpl() ;
        int count = adminService.update(user);
        if(count>0){
            //现在跳转分页查询地址上
            request.getRequestDispatcher("/admin?methodName=getUserByPage").forward(request,response);
        }
    }

    /**
     * 管理员删除用户
     * @param request 请求
     * @param response 响应
     */
    public void delUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //window.location.href="${pageContext.request.contextPath}/admin?methodName=delUser&uid="+uid;
        //获取uid参数
        String uid = request.getParameter("uid");
        //调用业务接口
        AdminService adminService = new AdminServiceImpl() ;
        int count = adminService.delete(uid);
        if(count>0){
            //删除成功
            //跳转到分页查询的后端地址
            request.getRequestDispatcher("/admin?methodName=getUserByPage").forward(request,response);
        }
    }
}
